<template>
  <!-- InformationType 资讯分类 单独二级列表 -->
  <PageContainer>
    <view class="container">
      <template v-if="isShowTab">
        <Tabs v-model="currentTab">
          <Header :title="pageTitle" :data="childrenTabs" :is-box-shadow="false">
            <view class="tabsBorder">
              <TabNav tabBarColor="#FFFFFF" tabActiveFontSize="32rpx" />
            </view>
          </Header>
          <template v-for="(item, index) in childrenTabs" :key="index">
            <TabsItem :name="item.module" :value="item.id"></TabsItem>
          </template>
        </Tabs>
      </template>

      <Header v-else :title="pageTitle"></Header>
      <!-- 列表 -->
      <Paglist
        v-model="dataList"
        :is-init-api="false"
        :parame="parame"
        @register="register"
        :is-show-loadmore="dataList.length > 1"
      >
        <template>
          <template
            v-if="
              dataList.length > 1 ||
              (dataList.length === 1 && dataList[0].linkType !== InformationLinkTypeEnum.TEXT)
            "
          >
            <view
              v-for="(v, i) in dataList"
              :key="v.id"
              class="px-15 bgf"
              :class="{ 'pt-15': i === 0 }"
            >
              <NewsItem :item="v" />
            </view>
          </template>
          <!-- 内容 -->
          <view v-else-if="dataList[0]" style="padding: 10rpx 40rpx; background-color: #fff">
            <TRichText v-if="dataList[0].havePermission" :content="dataList[0].details" />
            <Empty v-else text="您暂无查阅权限" type="noPermission" />
          </view>
        </template>
      </Paglist>
    </view>
  </PageContainer>
</template>

<script lang="ts" setup>
  /**  InformationType  资讯 */
  import PageContainer from '@/components/PageContainer/index.vue';
  import { informationPageList } from '@/api/home';
  import { InformationLinkTypeEnum, type InformationPageListResData } from '@/api/home/types';
  import Header from '@/components/Header/index.vue';
  import Paglist from '@/components/Paglist/index.vue';
  import { useConList } from '@/components/Paglist/useConList';
  import { provide, reactive, ref, unref, watch } from 'vue';
  import TRichText from '@/components/TRichText/index.vue';
  import { onLoad } from '@/router';
  import NewsItem from '@/components/NewsItem/index.vue';
  import Tabs from '@/components/tabs/index.vue';
  import TabsItem from '@/components/tabs/item.vue';
  import TabNav from '@/components/tabs/nav.vue';
  import { type InformationCategoryItem } from '@/api/dynamicAllocation/types';
  import { secondLevelCategoryList } from '@/api/dynamicAllocation';
  import { onPageScroll, onShareAppMessage } from '@dcloudio/uni-app';
  import { useAppStoreWithOut } from '@/store/modules/app';
  import { type InformationType } from '@/router/types/modules';
  import { useAppShare } from '@/hooks/core/useAppShare';
  import Empty from '@/components/Empty/Empty.vue';

  const { setWxSharePath } = useAppShare();
  provide('api', (obj: any) => {
    return informationPageList(obj);
  });

  const pageTitle = ref('');

  const dataList = ref<InformationPageListResData[]>([]);

  const [register, { refresh }] = useConList();

  const isShowTab = ref(false);
  const isInit = ref(false);
  const currentTab = ref(1);
  const moduleSubTitle = ref('');

  const childrenTabs = ref<InformationCategoryItem[]>([]);

  watch(currentTab, tabsChange);
  function tabsChange(id: number) {
    if (!isInit.value) return;
    parame.moduleSub = id;
    refresh && refresh();
  }

  const parame = reactive<{ module?: number; moduleSub?: number }>({
    module: undefined,
    moduleSub: undefined,
  });

  const query = ref<InformationType>();
  onLoad<'InformationType'>(async (op) => {
    const { moduleCode, childrens, subTitle, currentId } = op;
    query.value = op;
    parame.module = moduleCode;
    if (childrens?.length) {
      parame.moduleSub = childrens[0].id;
    } else {
      const subs = await secondLevelCategoryList(moduleCode);
      childrenTabs.value = subs;
      let code = subs[0]?.id ?? currentTab.value;
      if (currentId) code = currentId;
      currentTab.value = code;
      parame.moduleSub = code;
      if (subs?.length > 1) isShowTab.value = true;
    }
    const r = await refresh();

    if (op.pageTitle) {
      pageTitle.value = op.pageTitle;
    } else if (r.records.length <= 1 && childrenTabs.value.length <= 1) {
      pageTitle.value = '文章详情';
    } else {
      subTitle && (pageTitle.value = subTitle);
    }

    isInit.value = true;
  });

  provide('tabTitle', moduleSubTitle);
  provide('tabIsShow', false);
  onPageScroll(() => {});

  const { globalConfig } = useAppStoreWithOut();
  onShareAppMessage(() => ({
    imageUrl:
      unref(dataList).length == 1
        ? unref(dataList)[0].coverUrl
        : globalConfig?.comShareOptions.shareImg,
    path: setWxSharePath({ name: 'informationType', options: query.value as InformationType }),
    title: pageTitle.value || globalConfig?.comShareOptions.shareTitle,
  }));
</script>

<style lang="scss" scoped>
  .tabsBorder {
    border-bottom: solid 1px #f5f5f5;
    padding: 14rpx 0;
  }
</style>
